{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "<built-in function abs>\n",
      "<built-in function abs>\n"
     ]
    }
   ],
   "source": [
    "print(abs(-10))\n",
    "print(abs)\n",
    "x = abs\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "def add(x, y, f):\n",
    "    return f(x) + f(y)\n",
    "print(add(-1, 1, abs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 20, 40, 60, 80]\n",
      "25\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "L = [x for x in range(10) if x % 2 == 0]\n",
    "m = map(lambda x:x*10, L)\n",
    "print(list(m))\n",
    "\n",
    "from collections.abc import Iterator\n",
    "from functools import reduce\n",
    "\n",
    "sum = reduce(lambda x, y : x+y, [1, 3, 5, 7, 9])\n",
    "print(sum)\n",
    "\n",
    "print(isinstance(m, Iterator))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13579\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "list1 = [x for x in range(10) if x % 2 != 0]\n",
    "a = reduce(lambda x,y:(x * 10) + y, list1)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "0*2*4*6*8*\n",
      "0-2-4-6-8-"
     ]
    }
   ],
   "source": [
    "list2 = [x for x in range(10) ]\n",
    "l = filter(lambda x: x % 2 == 0, list2)\n",
    "print(list2)\n",
    "\n",
    "\n",
    "while True:\n",
    "    try:\n",
    "        item = next(l)  # 实际调用 l.__next__()函数\n",
    "        print(item, end=\"*\")\n",
    "    except StopIteration:\n",
    "        break\n",
    "print(\"\")\n",
    "\n",
    "l = filter(lambda x: x % 2 == 0, list2)\n",
    "for item in l:\n",
    "    print(item, end=\"-\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'C']\n"
     ]
    }
   ],
   "source": [
    "str1 = [\"A\", \"\", \"B\", None, \"C\", \" \"]\n",
    "l = filter(lambda x: x and x.strip(), str1)\n",
    "print(list(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'Adam', 'age': 92}, {'name': 'Lisa', 'age': 88}, {'name': 'Bob', 'age': 75}, {'name': 'Bart', 'age': 66}]\n",
      "[{'name': 'Adam', 'age': 92}, {'name': 'Bart', 'age': 66}, {'name': 'Bob', 'age': 75}, {'name': 'Lisa', 'age': 88}]\n",
      "[{'name': 'Adam', 'age': 92}, {'name': 'Bart', 'age': 66}, {'name': 'Bob', 'age': 75}, {'name': 'Lisa', 'age': 88}]\n",
      "[{'name': 'Bob', 'age': 75}, {'name': 'Bart', 'age': 66}]\n"
     ]
    }
   ],
   "source": [
    "students = [{\"name\":'Bob', \"age\":75}, {\"name\":'Adam', \"age\":92}, {\"name\":'Bart', \"age\":66}, {\"name\":'Lisa', \"age\":88}]\n",
    "print(sorted(students, key=lambda x:x['age'], reverse=True))\n",
    "print(sorted(students, key=lambda x:x['name']))\n",
    "print(sorted(students, key=lambda x:x['name'].lower()))\n",
    "\n",
    "print(list(filter(lambda x: x['age'] < 80, students)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果: 130\n",
      "func_in\n"
     ]
    }
   ],
   "source": [
    "def func_out(num1):\n",
    "    def func_in(num2):\n",
    "        # 内部函数修改外部函数的变量\n",
    "        nonlocal num1\n",
    "        num1 += 100\n",
    "        return num1 + num2\n",
    "    return func_in\n",
    "\n",
    "\n",
    "f = func_out(10)\n",
    "result = f(20)\n",
    "print(\"结果:\", result)\n",
    "print(f.__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0710678118654755\n",
      "据原点距离： 3.605551275463989\n",
      "据1点距离： 2.23606797749979\n",
      "(<cell at 0x7fb3f83c2ef0: int object at 0x87ec48>, <cell at 0x7fb3f83c3430: int object at 0x87ec48>)\n",
      "funcIn\n",
      "<class 'function'>\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def funcOut(x, y):\n",
    "    def funcIn(x1, y1):\n",
    "        return math.sqrt((x1 - x) ** 2 + (y1 - y) ** 2)\n",
    "    return funcIn\n",
    "\n",
    "funcIn = funcOut(0, 0)\n",
    "print(funcIn(5, 5))\n",
    "\n",
    "point0 = funcOut(0, 0)\n",
    "point1 = funcOut(1, 1)\n",
    "print(\"据原点距离：\", point0(3, 2))\n",
    "print(\"据1点距离：\", point1(3, 2))\n",
    "print(point0.__closure__)\n",
    "print(point0.__name__)\n",
    "print(type(point0))\n",
    "print(type(point0.__closure__))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "one\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "# def writeLog(func):\n",
    "#     with open(\"log.txt\", \"+a\") as filename:\n",
    "#         filename.write(\"\\n\")\n",
    "#         filename.write(func.__name__)\n",
    "#         filename.write(\"\\t\")\n",
    "#         filename.write(time.asctime())\n",
    "#         filename.write(\"\\n\")\n",
    "\n",
    "def funcOut(func):\n",
    "    def funcIn():\n",
    "        with open(\"log.txt\", \"+a\") as filename:\n",
    "            filename.write(\"\\n\")\n",
    "            filename.write(func.__name__)\n",
    "            filename.write(\"\\t\")\n",
    "            filename.write(time.asctime())\n",
    "            filename.write(\"\\n\")\n",
    "        func()\n",
    "    return funcIn\n",
    "\n",
    "@funcOut\n",
    "def print1():\n",
    "    print(\"one\")\n",
    "\n",
    "@funcOut\n",
    "def print2():\n",
    "    print(\"two\")\n",
    "\n",
    "\n",
    "print1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wrapper\n",
      "this is wrapper\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "使用装饰器后，函数名和函数注释的变化\n",
    "\"\"\"\n",
    "\n",
    "def outFunc(func):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        \"\"\"this is wrapper\"\"\"\n",
    "        res = func(*args, **kwargs)\n",
    "        return res\n",
    "    return wrapper\n",
    "\n",
    "@outFunc\n",
    "def hello():\n",
    "    \"\"\"this is hello\"\"\"\n",
    "    print(\"hello\")\n",
    "\n",
    "\n",
    "print(hello.__name__)\n",
    "print(hello.__doc__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "this is hello\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "使用装饰器后，利用伪装，不改变原函数名和函数注释\n",
    "\"\"\"\n",
    "import functools\n",
    "\n",
    "def outFunc(func):\n",
    "    @functools.wraps(func) # 伪装不改变原函数\n",
    "    def wrapper(*args, **kwargs):\n",
    "        \"\"\"this is wrapper\"\"\"\n",
    "        res = func(*args, **kwargs)\n",
    "        return res\n",
    "\n",
    "    return wrapper\n",
    "\n",
    "\n",
    "@outFunc\n",
    "def hello():\n",
    "    \"\"\"this is hello\"\"\"\n",
    "    print(\"hello\")\n",
    "\n",
    "\n",
    "print(hello.__name__)\n",
    "print(hello.__doc__)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
